home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / MacPerl 506 appl folder.sit / MacPerl 506 appl folder / Mac_Perl_506r1m_appl / lib / bigfloat.pl < prev    next >
Perl Script  |  1995-03-19  |  7KB  |  234 lines

  1. package bigfloat;
  2. require "bigint.pl";
  3. # Arbitrary length float math package
  4. #
  5. # by Mark Biggar
  6. #
  7. # number format
  8. #   canonical strings have the form /[+-]¥d+E[+-]¥d+/
  9. #   Input values can have inbedded whitespace
  10. # Error returns
  11. #   'NaN'           An input parameter was "Not a Number" or 
  12. #                       divide by zero or sqrt of negative number
  13. # Division is computed to 
  14. #   max($div_scale,length(dividend)+length(divisor)) 
  15. #   digits by default.
  16. # Also used for default sqrt scale
  17.  
  18. $div_scale = 40;
  19.  
  20. # Rounding modes one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'.
  21.  
  22. $rnd_mode = 'even';
  23.  
  24. #   bigfloat routines
  25. #
  26. #   fadd(NSTR, NSTR) return NSTR            addition
  27. #   fsub(NSTR, NSTR) return NSTR            subtraction
  28. #   fmul(NSTR, NSTR) return NSTR            multiplication
  29. #   fdiv(NSTR, NSTR[,SCALE]) returns NSTR   division to SCALE places
  30. #   fneg(NSTR) return NSTR                  negation
  31. #   fabs(NSTR) return NSTR                  absolute value
  32. #   fcmp(NSTR,NSTR) return CODE             compare undef,<0,=0,>0
  33. #   fround(NSTR, SCALE) return NSTR         round to SCALE digits
  34. #   ffround(NSTR, SCALE) return NSTR        round at SCALEth place
  35. #   fnorm(NSTR) return (NSTR)               normalize
  36. #   fsqrt(NSTR[, SCALE]) return NSTR        sqrt to SCALE places
  37. # Convert a number to canonical string form.
  38. #   Takes something that looks like a number and converts it to
  39. #   the form /^[+-]¥d+E[+-]¥d+$/.
  40. sub main'fnorm { #(string) return fnum_str
  41.     local($_) = @_;
  42.     s/¥s+//g;                               # strip white space
  43.     if (/^([+-]?)(¥d*)(¥.(¥d*))?([Ee]([+-]?¥d+))?$/ && "$2$4" ne '') {
  44.     &norm(($1 ? "$1$2$4" : "+$2$4"),(($4 ne '') ? $6-length($4) : $6));
  45.     } else {
  46.     'NaN';
  47.     }
  48. }
  49.  
  50. # normalize number -- for internal use
  51. sub norm { #(mantissa, exponent) return fnum_str
  52.     local($_, $exp) = @_;
  53.     if ($_ eq 'NaN') {
  54.     'NaN';
  55.     } else {
  56.     s/^([+-])0+/$1/;                        # strip leading zeros
  57.     if (length($_) == 1) {
  58.         '+0E+0';
  59.     } else {
  60.         $exp += length($1) if (s/(0+)$//);  # strip trailing zeros
  61.         sprintf("%sE%+ld", $_, $exp);
  62.     }
  63.     }
  64. }
  65.  
  66. # negation
  67. sub main'fneg { #(fnum_str) return fnum_str
  68.     local($_) = &'fnorm($_[$[]);
  69.     vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0E+0'; # flip sign
  70.     s/^H/N/;
  71.     $_;
  72. }
  73.  
  74. # absolute value
  75. sub main'fabs { #(fnum_str) return fnum_str
  76.     local($_) = &'fnorm($_[$[]);
  77.     s/^-/+/;                               # mash sign
  78.     $_;
  79. }
  80.  
  81. # multiplication
  82. sub main'fmul { #(fnum_str, fnum_str) return fnum_str
  83.     local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
  84.     if ($x eq 'NaN' || $y eq 'NaN') {
  85.     'NaN';
  86.     } else {
  87.     local($xm,$xe) = split('E',$x);
  88.     local($ym,$ye) = split('E',$y);
  89.     &norm(&'bmul($xm,$ym),$xe+$ye);
  90.     }
  91. }
  92. # addition
  93. sub main'fadd { #(fnum_str, fnum_str) return fnum_str
  94.     local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
  95.     if ($x eq 'NaN' || $y eq 'NaN') {
  96.     'NaN';
  97.     } else {
  98.     local($xm,$xe) = split('E',$x);
  99.     local($ym,$ye) = split('E',$y);
  100.     ($xm,$xe,$ym,$ye) = ($ym,$ye,$xm,$xe) if ($xe < $ye);
  101.     &norm(&'badd($ym,$xm.('0' x ($xe-$ye))),$ye);
  102.     }
  103. }
  104.  
  105. # subtraction
  106. sub main'fsub { #(fnum_str, fnum_str) return fnum_str
  107.     &'fadd($_[$[],&'fneg($_[$[+1]));    
  108. }
  109.  
  110. # division
  111. #   args are dividend, divisor, scale (optional)
  112. #   result has at most max(scale, length(dividend), length(divisor)) digits
  113. sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str
  114. {
  115.     local($x,$y,$scale) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]),$_[$[+2]);
  116.     if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') {
  117.     'NaN';
  118.     } else {
  119.     local($xm,$xe) = split('E',$x);
  120.     local($ym,$ye) = split('E',$y);
  121.     $scale = $div_scale if (!$scale);
  122.     $scale = length($xm)-1 if (length($xm)-1 > $scale);
  123.     $scale = length($ym)-1 if (length($ym)-1 > $scale);
  124.     $scale = $scale + length($ym) - length($xm);
  125.     &norm(&round(&'bdiv($xm.('0' x $scale),$ym),$ym),
  126.         $xe-$ye-$scale);
  127.     }
  128. }
  129. # round int $q based on fraction $r/$base using $rnd_mode
  130. sub round { #(int_str, int_str, int_str) return int_str
  131.     local($q,$r,$base) = @_;
  132.     if ($q eq 'NaN' || $r eq 'NaN') {
  133.     'NaN';
  134.     } elsif ($rnd_mode eq 'trunc') {
  135.     $q;                         # just truncate
  136.     } else {
  137.     local($cmp) = &'bcmp(&'bmul($r,'+2'),$base);
  138.     if ( $cmp < 0 ||
  139.          ($cmp == 0 &&
  140.           ( $rnd_mode eq 'zero'                             ||
  141.            ($rnd_mode eq '-inf' && (substr($q,$[,1) eq '+')) ||
  142.            ($rnd_mode eq '+inf' && (substr($q,$[,1) eq '-')) ||
  143.            ($rnd_mode eq 'even' && $q =~ /[24680]$/)        ||
  144.            ($rnd_mode eq 'odd'  && $q =~ /[13579]$/)        )) ) {
  145.         $q;                     # round down
  146.     } else {
  147.         &'badd($q, ((substr($q,$[,1) eq '-') ? '-1' : '+1'));
  148.                     # round up
  149.     }
  150.     }
  151. }
  152.  
  153. # round the mantissa of $x to $scale digits
  154. sub main'fround { #(fnum_str, scale) return fnum_str
  155.     local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
  156.     if ($x eq 'NaN' || $scale <= 0) {
  157.     $x;
  158.     } else {
  159.     local($xm,$xe) = split('E',$x);
  160.     if (length($xm)-1 <= $scale) {
  161.         $x;
  162.     } else {
  163.         &norm(&round(substr($xm,$[,$scale+1),
  164.              "+0".substr($xm,$[+$scale+1,1),"+10"),
  165.           $xe+length($xm)-$scale-1);
  166.     }
  167.     }
  168. }
  169. # round $x at the 10 to the $scale digit place
  170. sub main'ffround { #(fnum_str, scale) return fnum_str
  171.     local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
  172.     if ($x eq 'NaN') {
  173.     'NaN';
  174.     } else {
  175.     local($xm,$xe) = split('E',$x);
  176.     if ($xe >= $scale) {
  177.         $x;
  178.     } else {
  179.         $xe = length($xm)+$xe-$scale;
  180.         if ($xe < 1) {
  181.         '+0E+0';
  182.         } elsif ($xe == 1) {
  183.         &norm(&round('+0',"+0".substr($xm,$[+1,1),"+10"), $scale);
  184.         } else {
  185.         &norm(&round(substr($xm,$[,$xe),
  186.               "+0".substr($xm,$[+$xe,1),"+10"), $scale);
  187.         }
  188.     }
  189.     }
  190. }
  191.     
  192. # compare 2 values returns one of undef, <0, =0, >0
  193. #   returns undef if either or both input value are not numbers
  194. sub main'fcmp #(fnum_str, fnum_str) return cond_code
  195. {
  196.     local($x, $y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
  197.     if ($x eq "NaN" || $y eq "NaN") {
  198.     undef;
  199.     } else {
  200.     ord($y) <=> ord($x)
  201.     ||
  202.     (  local($xm,$xe,$ym,$ye) = split('E', $x."E$y"),
  203.          (($xe <=> $ye) * (substr($x,$[,1).'1')
  204.              || &bigint'cmp($xm,$ym))
  205.     );
  206.     }
  207. }
  208. # square root by Newtons method.
  209. sub main'fsqrt { #(fnum_str[, scale]) return fnum_str
  210.     local($x, $scale) = (&'fnorm($_[$[]), $_[$[+1]);
  211.     if ($x eq 'NaN' || $x =~ /^-/) {
  212.     'NaN';
  213.     } elsif ($x eq '+0E+0') {
  214.     '+0E+0';
  215.     } else {
  216.     local($xm, $xe) = split('E',$x);
  217.     $scale = $div_scale if (!$scale);
  218.     $scale = length($xm)-1 if ($scale < length($xm)-1);
  219.     local($gs, $guess) = (1, sprintf("1E%+d", (length($xm)+$xe-1)/2));
  220.     while ($gs < 2*$scale) {
  221.         $guess = &'fmul(&'fadd($guess,&'fdiv($x,$guess,$gs*2)),".5");
  222.         $gs *= 2;
  223.     }
  224.     &'fround($guess, $scale);
  225.     }
  226. }
  227.  
  228. 1;
  229.